"""文件交换器，多端接收"""

import requests
from src.core import log
from flask import request
from src.core.variables.status import *
from src.core.variables.constant import *
from pydantic import ValidationError
from src.schema.file_exchanger import file_exchanger_schema


def receiver() -> bool:
    """简单的文件接收器"""
    json_data = request.get_json()
    file = request.files.get("file")
    try:
        schema = file_exchanger_schema(**request.get_json(force=True))
        if schema.running_pool in ["Java"]:
            file.save(f"{CONF.PATH.RUN_JAVA}{schema.time_dir}/{file.filename}")
        elif schema.running_pool in ["Web"]:
            file.save(f"{CONF.PATH.RUN_STATIC}{schema.time_dir}/{file.filename}")
        elif schema.running_pool in [""]:
            file.save(f"{CONF.PATH.REPO}{schema.time_dir}/{file.filename}")
        return SUCCESS_CODE_CUSTOMIZE("接收文件成功", 200)
    except ValidationError as e:
        log.ERROR(f"接收文件失败: {e.errors()}")
        return ERROR_CODE_CUSTOMIZE(f"接收文件失败: {e.errors()}", 400)
    except Exception as e:
        log.ERROR(f"接收文件失败: {e}")
        return ERROR_CODE_CUSTOMIZE(f"接收文件失败: {e}", 500)
